package com.cpi.cornpp.transports.action;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.struts.Globals;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.util.MessageResources;

import com.cpi.cornpp.common.filter.BreadCrumbList;
import com.cpi.cornpp.transports.comparator.CostElementAddedComparator;
import com.cpi.cornpp.transports.exception.TransportException;
import com.cpi.cornpp.transports.form.TransportForm;
import com.cpi.cornpp.transports.view.CostElementView;
import com.cpi.cornpp.transports.view.TransportView;
import com.cs.common.init.beans.SiteMapDTO;

public class AddTransportCostElementAction extends Action {
	private final static String CUSTOM_EXCEPTION = "custom_exception";//$NON-NLS-1$
	private final static String ERROR_MESSAGE = "ERROR_MESSAGE";//$NON-NLS-1$
	private static final String SHOW_NEW_TRANSPORT_FORWARD = "showNewTransport";
	private static final String SHOW_EDIT_TRANSPORT_FORWARD = "showEditTransport";
	private static final String FAILURE_FORWARD = "error";
	private static final String COMMON_ERROR_MESSAGE_KEY = "error.message.common.unexpectedError";

	public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws TransportException {
		String messageError = null;
		int selectedCostElementId = 0;
		double selectedCostElementAmount = 0.0D;
		TransportForm transportForm = null;
		TransportView transportView = null;
		CostElementView costElementView = null;
		List costElementsView = null;
		List addedTransportCostElements = null;
		HttpSession session = null;
		ActionForward forward = null;
		MessageResources messageResources = null;
		
		forward = mapping.findForward(FAILURE_FORWARD);
		
		try{
			
			session = request.getSession(false);
			messageResources = getResources(request);
			transportView = (TransportView) session.getAttribute("transportView");
			costElementsView = (List) session.getAttribute("costElementsList");
			addedTransportCostElements = (List) session.getAttribute("addedTransportCostElements");
			
			if ((form != null) && (transportView != null)){
				transportForm = (TransportForm) form;
				selectedCostElementId = transportForm.getCostElementView().getChargeTypeId();
				selectedCostElementAmount = transportForm.getCostElementView().getChargeTypeCost();
				
				addedTransportCostElements = (CollectionUtils.isNotEmpty(addedTransportCostElements)) ? addedTransportCostElements : new ArrayList(0);
				
				if (CollectionUtils.isNotEmpty(costElementsView)){
					
					for (Iterator itr = costElementsView.iterator(); itr.hasNext(); ){
						costElementView = (CostElementView) itr.next();
						
						if (costElementView.getChargeTypeId() == selectedCostElementId){
							costElementView.setChargeTypeCost(selectedCostElementAmount);
							transportView.getTransportCostElements().add(costElementView);
							addedTransportCostElements.add(costElementView);
							break;
						}
					}
				}
				Collections.sort(transportView.getTransportCostElements(), new CostElementAddedComparator());
				session.setAttribute("transportView", transportView);
				session.setAttribute("addedTransportCostElements", addedTransportCostElements);
				request.setAttribute("transportView", transportView);
				
				forward = (StringUtils.isNotEmpty(transportForm.getId())) ? mapping.findForward(SHOW_EDIT_TRANSPORT_FORWARD) : mapping.findForward(SHOW_NEW_TRANSPORT_FORWARD);
				
				//FIXME
				BreadCrumbList breadCrumbList = new BreadCrumbList(0);
				SiteMapDTO siteMapDTO = new SiteMapDTO();
				
				siteMapDTO.setLabel("label.breadcrum.home");
				siteMapDTO.setUrl("welcome.do");
				breadCrumbList.add(siteMapDTO);
				
				siteMapDTO = new SiteMapDTO();
				
				siteMapDTO.setLabel("label.breadcrum.transport.list");
				siteMapDTO.setUrl("showTransportQuery.do");
				breadCrumbList.add(siteMapDTO);
				
				breadCrumbList.setLabel((StringUtils.isNotEmpty(transportForm.getId())) ? "label.breadcrum.transport.edit" : "label.breadcrum.transport.new");
				
				session.setAttribute("BREADCRUMBS", breadCrumbList);
				session.setAttribute("BREADCRUMB_LABEL", breadCrumbList.getLabel());
			} else {
				TransportException tre = new TransportException(COMMON_ERROR_MESSAGE_KEY, TransportException.LAYER_ACTION, TransportException.ACTION_INSERT);
				
				throw tre;
			}
			
		} catch(TransportException tre){
			messageError = messageResources.getMessage((Locale)session.getAttribute(Globals.LOCALE_KEY), tre.getErrorCode());
			
			if(null == messageError){
				session.setAttribute(CUSTOM_EXCEPTION, tre);
				form.reset(mapping, request);
				throw tre;
			}else{
				session.setAttribute(ERROR_MESSAGE, messageError);
				forward = mapping.findForward(FAILURE_FORWARD);
			}
			
		} catch(Exception e){
			TransportException tre = new TransportException(e , TransportException.LAYER_ACTION, TransportException.ACTION_INSERT);;
			
			session.setAttribute(CUSTOM_EXCEPTION, tre);
			form.reset(mapping, request);
			e.printStackTrace(System.out);
			
			throw tre;
		} finally {
			session.removeAttribute("costElementsList");
		}
		
		return forward;
	}
}